package com.example.dsplatform.Service.ipml;

import com.example.dsplatform.Dao.Mapper.UserMapper;
import com.example.dsplatform.Service.LoginService;
import com.example.dsplatform.pojo.Entity.LoginUser;
import com.example.dsplatform.pojo.Entity.ResponseResult;
import com.example.dsplatform.pojo.Entity.User;
import com.example.dsplatform.utls.JwtUtil;
import com.example.dsplatform.utls.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
public class LoginServiceIpml implements LoginService {


    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private UserMapper userMapper;


    @Override
    public ResponseResult login(User user) {
        String mes_id = user.getId();
        String mes_phone = user.getPhone();
        String password = user.getPassword();

        String mes;
        if (mes_id == null) {
            mes = mes_phone + 'p';
        } else {
            mes = mes_id + 'i';
        }


        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(mes, password);
        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
        if (Objects.isNull(authenticate)) {
            System.out.println("出错");
            throw new RuntimeException("用户名或密码错误");
        }

        LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
        String userId = loginUser.getUser().getId().toString();
        /*String userName = loginUser.getUser().getUsername().toString();
        String msg = "id:"+userId+"username"+userName;*/
        String jwt = JwtUtil.createJWT(userId);
        redisCache.setCacheObject("login:" + userId, loginUser);
        HashMap<String, String> map = new HashMap<>();

        map.put("token", jwt);
        return new ResponseResult(200, "登陆成功", map);
    }

    @Override
    public ResponseResult loginout() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userid = loginUser.getUser().getId();
        redisCache.deleteObject("login:" + userid);
        return new ResponseResult(200, "退出成功");
    }

    @Override
    public ResponseResult active(User user) {
        String state = userMapper.select_State(user.getId(),user.getUsername(),user.getIdentity());
        if ("1".equals(state)){
            return new ResponseResult(201, "该账号已激活");
        }
        else if("0".equals(state)) {
            userMapper.active_State(user.getId(),user.getUsername(),user.getIdentity());
            return new ResponseResult(200, "激活成功");
        }
        else  {
            return new ResponseResult(204, "该账号不存在");
        }

    }

    public ResponseResult setPassword(User user) {
        String list = userMapper.selectbyPhone(user.getPhone());
        if(list == null){
            return new ResponseResult(204, "没有这个账户");
        }

        else{
            String password= user.getPassword();

        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String passwordE = passwordEncoder.encode(password);
        userMapper.setPasswordByPhone(user.getPhone(),passwordE);
        return new ResponseResult(200, "设置密码完成");
        }
    }
    @Override
    public ResponseResult selectById(String id) {
        List<String> list  = (List<String>) userMapper.selectById(id);
        return new ResponseResult(200, list);

    }


}
